package com.yjh.server.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yjh.pojo.entity.ReservationTime;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ReservationTimeMapper extends BaseMapper<ReservationTime> {
    // 自定义查询：根据球场ID和日期查询可预约时段
//    @Select("SELECT rt.time_id, rt.time_slot, rt.start_hour, rt.end_hour " +
//            "FROM reservation_time rt " +
//            "LEFT JOIN reservation_main rm " +
//            "ON rt.time_id = rm.time_id " +
//            "AND rm.court_id = #{courtId} " +
//            "AND rm.reserve_date = #{reserveDate} " +
//            "AND rm.reserve_status != 2 " + // 排除已取消的预约
//            "WHERE rm.reserve_id IS NULL") // 未被预约的时段
//    List<ReservationTime> selectAvailableSlots(
//            @Param("courtId") Integer courtId,
//            @Param("reserveDate") LocalDate reserveDate);
    @Select("SELECT rt.time_id, rt.time_slot, rt.start_hour, rt.end_hour " +
            "FROM reservation_time rt " +
            "INNER JOIN reserve_time_relation rtr " +  // 关联中间表
            "ON rt.time_id = rtr.time_id " +
            "INNER JOIN reservation_main rm " +            // 关联预约主表
            "ON rtr.reserve_id = rm.reserve_id " +         // 通过中间表的 reserve_id 关联主表
            "WHERE rm.court_id = #{courtId} " +            // 原条件：场馆ID
            "AND rm.reserve_date = #{reserveDate} " +      // 原条件：预约日期
            "AND rm.reserve_status != 2")                  // 原条件：排除状态为2的预约
    List<ReservationTime> selectInAvailableSlots(
            @Param("courtId") Integer courtId,
            @Param("reserveDate") LocalDate reserveDate);
}
